package server

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"user-service/basic/config"
	__ "user-service/basic/proto"
	"user-service/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

func (s *Server) SendSms(_ context.Context, in *__.SendSmsReq) (*__.SendSmsResp, error) {
	code := rand.Intn(9000) + 100
	//sms := pkg.SmsFort.CreateSms("Aliyun")
	//sendSms, _ := sms.SendSms(in.Phone, strconv.Itoa(code))
	//if *sendSms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sendSms.Body.Message)
	//}
	config.Rdb.Set(config.Ctx, "sendSms"+in.Phone, code, time.Minute*5)
	return &__.SendSmsResp{}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) GetPhoneByPhone(_ context.Context, in *__.GetPhoneByPhoneReq) (*__.GetPhoneByPhoneResp, error) {
	var user model.User
	if err := user.GetPhoneByPhone(config.DB, in.Phone); err != nil {
		return nil, err
	}
	return &__.GetPhoneByPhoneResp{
		Phone:  user.Phone,
		UserId: int64(user.ID),
		Email:  user.Email,
		Image:  user.Image,
	}, nil
}
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	result, _ := config.Rdb.Get(config.Ctx, "sendSms"+in.Phone).Result()
	if result != in.Code {
		return nil, fmt.Errorf("验证码已失效")
	}
	user := model.User{
		Phone:    in.Phone,
		Password: in.Password,
	}
	if err := user.CreateUser(config.DB); err != nil {
		return nil, err
	}
	return &__.CreateUserResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *Server) TypeList(_ context.Context, in *__.TypeListReq) (*__.TypeListResp, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	var types []model.Type
	offset := (page - 1) * pageSize
	config.DB.Offset(int(offset)).Limit(int(pageSize)).Where("type_name LIKE ?", "%"+in.TypeName+"%").Find(&types)
	var typelist []*__.TypeList
	for _, m := range types {
		list := &__.TypeList{
			TypeName: m.TypeName,
		}
		typelist = append(typelist, list)
	}
	return &__.TypeListResp{
		List: typelist,
	}, nil
}
func (s *Server) GoodDetail(_ context.Context, in *__.GoodDetailReq) (*__.GoodDetailResp, error) {
	var good []model.Good
	config.DB.Where("id=?", in.Id).Find(&good)

	var typelist []*__.GoodDetail
	config.Rdb.SetNX(config.Ctx, "good", typelist, time.Minute*5)
	for _, m := range good {
		list := &__.GoodDetail{
			UserId:       m.UserId,
			Title:        m.Title,
			Image:        m.Image,
			LikeCount:    int64(m.LikeCount),
			CommentId:    m.CommentId,
			CollectCount: m.CollectCount,
			ZhuBo:        m.Zhubo,
			Price:        m.Price,
		}
		typelist = append(typelist, list)
	}
	config.Rdb.Del(config.Ctx, "good")
	return &__.GoodDetailResp{
		List: typelist,
	}, nil
}
func (s *Server) CommentCreate(_ context.Context, in *__.CommentCreateReq) (*__.CommentCreateResp, error) {
	comment := model.Comment{
		UserId:  in.UserId,
		GoodId:  in.GoodId,
		ZhuBoId: in.ZhuBoId,
		Comment: in.Comment,
	}
	if err := comment.CreateComment(config.DB); err != nil {
		return nil, err
	}
	return &__.CommentCreateResp{
		CommentId: int64(comment.ID),
	}, nil
}
